package com.viphuli.app.tool.handler;

import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.share.wxapi.OpenLoginListener;
import com.offroader.share.wxapi.ShareUtils;
import com.offroader.utils.StringUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.activity.MyToolBarActivity;
import com.viphuli.app.tool.bean.page.AccountPage;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.Constants;
import com.viphuli.app.tool.common.MyApp;
import com.viphuli.app.tool.fragment.AccountBindPhoneFragment;
import com.viphuli.app.tool.fragment.AccountFragment;
import com.viphuli.app.tool.http.ApiRequest;
import com.viphuli.app.tool.utils.ImageUtils;

public class AccountResponseHandler extends MyBaseHttpResponseHandler<AccountFragment, AccountPage> {

	@Override
	public void deal() {

		AccountUser user = page.getUser();

		caller.setUser(user);

		if (user != null) {

			ImageUtils.load(caller.getPortrait(), user.getPortrait());
			caller.getName().setText(user.getUserName());
			caller.getUserType().setText(user.userType());

			if (user.getBindPhone() == 1) {//已绑定手机
				caller.getPhone().setText(user.getTel());
			} else {
				caller.getPhone().setText("绑定");
				caller.getPhone().setTextColor(caller.getResources().getColor(R.color.cl_green));
			}

			if (user.getBindWechat() == 1) {//已绑定微信
				caller.getWechat().setText("已授权");
			} else {
				caller.getWechat().setText("授权");
				caller.getWechat().setTextColor(caller.getResources().getColor(R.color.cl_green));
			}

		}
		
		//当访问其它用户信息时
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(caller.getActivity());
		if (tokenKeeper.isLogin()) {

			if (tokenKeeper.isLogin() && !tokenKeeper.getOpenId().equals(user.getUid())) {
				if (caller.getActivity() instanceof MyToolBarActivity) {

					//标题
					MyToolBarActivity act = (MyToolBarActivity) caller.getActivity();
					act.setTitle(user.getUserName());

				}
			}

		}

	}

	@OnClick(R.id.id_account_phone_layout)
	public void bindPhone() {

		if (page.getUser().getBindPhone() == 0) {
			AccountBindPhoneFragment.go(caller.getActivity(), Constants.ACTIVITY_REQUEST_REFREASH_CODE);
		}

	}

	@OnClick(R.id.id_account_bind_wechat_layout)
	public void bindWechat() {

		if (page.getUser().getBindWechat() == 0) {

			String appId = MyApp.getInstance().getString(R.string.weichat_app_id);
			String appSecret = MyApp.getInstance().getString(R.string.weichat_app_secret);

			ShareUtils.loginWechat(caller.getActivity(), appId, appSecret, new OpenLoginListener() {

				@Override
				public void onSuccess(String accessToken, String openId) {

					if (StringUtils.isNotBlank(accessToken) && StringUtils.isNotBlank(openId)) {

						RequestParams params = new RequestParams();
						params.put("uid", page.getUser().getUid());
						params.put("openid", openId);
						params.put("access_token", accessToken);

						ApiRequest.accountBindWechat.request(caller, params, HandlerType.waitting);

					}

				}
			});

		}

	}

}
